package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 北国网
 * @author Persh
 *
 */
public class LndNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://[a-z]*.lnd.com.cn/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h6").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1.p_center").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("div.title1").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("p.biaot").text();
					if(StringUtils.isBlank(title)){
						title =doc.select("h4").text();
						if(StringUtils.isBlank(title)){
							title =doc.select("strong font[size=3]").text();
							if(StringUtils.isBlank(title)){
								title =doc.select("td[align=center] strong").text();
								if(StringUtils.isBlank(title)){
									title =doc.select("td[height=50px;] span").text();
									if(StringUtils.isBlank(title)){
										title =doc.select("div.contentnw h6").text();
									}
								}
							}
						}
					}
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("span.hj"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.nppo"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.zhenwen"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("span.hj1"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("span.hj11"));
						if(StringUtils.isBlank(content)){
							content=HtmlCleaner.getContentHtml(url,doc.select("span.hj"));
						}
					}
				}
			}
		}
		date=DateUtils.matchDate(doc.select("p.juzhong").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("p.crv").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.laiyuan").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("p.p_center").text());
						if(date==null){
							date=DateUtils.matchDate(url);
						}
				}
			}
		}
		if(StringUtils.isBlank(title)){
		}
		author=doc.select("p.p_center").text();
		author=StringUtils.substringBetween(author, "来源：", "作者：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		news.setAuthor(author);
		return news;
	}
	
	public static void main(String[] args) {
		LndNewsAnalyse analyse=new LndNewsAnalyse();
		String url="http://news.lnd.com.cn/htm/2014-05/20/content_4023633.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
